import { Redirect, Route, Switch } from 'react-router-dom'
import router from '@/router'
import PrivateRouter from './PrivateRouter'
import NotFound from '@/pages/NotFound'

const renderRoute = (router) => {
  let routeArr = []
  router.forEach(route => {
    if (!route.children || !route.children.length) {
      routeArr.push(<PrivateRouter key={route.path} path={route.path} component={route.component} />)
    } else if (route.children && !!route.children.length) {
      routeArr = routeArr.concat(renderRoute(route.children))
    }
  })
  return routeArr
}

const ContainerMain = () => {
  return (
    <Switch>
      <Route path="/app" exact render={()=><Redirect to="/app/home" />} />
      {renderRoute(router)}
      <Route component={NotFound} />
    </Switch>
  )
}

export default ContainerMain